/**
 * Created by root on 17-7-28.
 */
var http = require('../utils/http');
var logger = require('../config/log4js');
var cheerio = require('cheerio');
var ipListService = require('../service/ipListService');
var request = require("request");

// var ipListService = require('../service/ipListService');


// (function () {
//     var headers = {
//         'Cache-Control' : 'max-age=0',
//         'Upgrade-Insecure-Requests' : '1',
//         'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36',
//         'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
//         'Accept-Encoding' : 'gzip, deflate',
//         'Accept-Language' : 'zh-CN,zh;q=0.8',
//         'Cookie' : 'auth=8719e91c79c1e98cdf4d18b7f646eff9; UM_distinctid=15d972ee23464d-09f2f921e7a057-38710357-1fa400-15d972ee235a5f; JSESSIONID=0E7686745019A10AD9388C4E8AC346AD; CNZZDATA1260383977=1022195651-1501476052-null%7C1501667636; Hm_lvt_3406180e5d656c4789c6c08b08bf68c2=1501480018,1501584730,1501660440; Hm_lpvt_3406180e5d656c4789c6c08b08bf68c2=1501671260'
//     };
//     http.httpGetByHeaders("www.data5u.com/free/index.html", headers, function (result) {
//         logger.access.info(result);
//         if (result.statusCode === 200) {
//             var $ = cheerio.load(result.body);
//
//         } else {
//             logger.error.info(result);
//         }
//     });
// })();

//
// ipListDao.queryIP('113.68.65.122','8888', function (result) {
//     console.log(result);
// })

//
(async function () {

    // var ipResult = await ipListDao.queryIP('127.0.0.1', 80, function (result) {});

    // ipListService.addIP({ip : '127.0.0.1', port : 80, type : null}, function (result) {});
    // ipListDao.queryIP('127.0.0.1', '80', function (result) {
    //
    // });

    // http.data5uGet('http://www.kuaidaili.com/free/inha/', function (result) {
    //     if (result.statusCode === 200) {
    //         var $ = cheerio.load(result.body);
    //         $('tr').each(function () {
    //             var ipList = $(this).children();
    //             if ($(ipList[0]).text() !== 'IP') {
    //                 console.log($(ipList[0]).text() + ':' + $(ipList[1]).text() + ' ' + $(ipList[3]).text());
    //             }
    //         });
    //
    //     } else {
    //         logger.error.info(result);
    //     }
    // });

    // http.verifyHttp('180.118.243.252', 808, function (result) {
    //     console.log(result);
    // });
    
    
    // http.httpGetByHeaders('www.lagou.com/zhaopin/Java/?labelWords=label?labelWords=hot', {
    //     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    //     'Accept-Encoding':'gzip, deflate, br',
    //     'Accept-Language':'zh-CN,zh;q=0.8',
    //     'Cache-Control':'max-age=0',
    //     'Connection':'keep-alive',
    //     'Cookie':'user_trace_token=20170730133405-b3d5c736-74e8-11e7-bd27-5254005c3644; LGUID=20170730133405-b3d5ccb1-74e8-11e7-bd27-5254005c3644; JSESSIONID=ABAAABAACDBABJB7119030A915FB8A332477F67E3906C5E; _gat=1; PRE_UTM=m_cf_cpt_baidu_pc; PRE_HOST=bzclk.baidu.com; PRE_SITE=http%3A%2F%2Fbzclk.baidu.com%2Fadrc.php%3Ft%3D06KL00c00fATEwT0J_Kg0FNkUsKZGcuu00000rhhYH300000xgI9qg.THL0oUhY1x60UWdBmy-bIy9EUyNxTAT0T1d9uWfknHmvuj0snjckPhc30ZRqPYD3PD7AfbnknHPjPWcLwRmLrjc1PYFafbc4wRnYnHT0mHdL5iuVmv-b5Hc4P1bsrj0snWchTZFEuA-b5HDv0ARqpZwYTZnlQzqLILT8UA7MULR8mvqVQ1qdIAdxTvqdThP-5ydxmvuxmLKYgvF9pywdgLKW0APzm1YzPj6zrf%26tpl%3Dtpl_10085_15673_1%26l%3D1053927145%26attach%3Dlocation%253D%2526linkName%253D%2525E6%2525A0%252587%2525E9%2525A2%252598%2526linkText%253D%2525E3%252580%252590%2525E6%25258B%252589%2525E5%25258B%2525BE%2525E7%2525BD%252591%2525E3%252580%252591%2525E5%2525AE%252598%2525E7%2525BD%252591-%2525E4%2525B8%252593%2525E6%2525B3%2525A8%2525E4%2525BA%252592%2525E8%252581%252594%2525E7%2525BD%252591%2525E8%252581%25258C%2525E4%2525B8%25259A%2525E6%25259C%2525BA%2526xp%253Did%28%252522m260704b2%252522%29%25252FDIV%25255B1%25255D%25252FDIV%25255B1%25255D%25252FDIV%25255B1%25255D%25252FDIV%25255B1%25255D%25252FH2%25255B1%25255D%25252FA%25255B1%25255D%2526linkType%253D%2526checksum%253D128%26ie%3Dutf-8%26f%3D8%26tn%3Dbaiduhome_pg%26wd%3D%25E6%258B%2589%25E5%258B%25BE%25E7%25BD%2591%26oq%3Dgithub%26rqlang%3Dcn%26inputT%3D236449%26bs%3Dgithub; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2F%3Futm_source%3Dm_cf_cpt_baidu_pc; X_HTTP_TOKEN=cf364edbe6026ed7d73087942d92fc0b; _putrc=E7E3D8878C61FA69; login=true; unick=%E4%BD%95%E6%97%AD; showExpriedIndex=1; showExpriedCompanyHome=1; showExpriedMyPublish=1; hasDeliver=2; TG-TRACK-CODE=index_hotsearch; SEARCH_ID=819f399d83924e31954f4c56b3fc5ab8; index_location_city=%E6%B7%B1%E5%9C%B3; _ga=GA1.2.1105885630.1501392846; _gid=GA1.2.1446168484.1501996107; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1501392846,1501996107; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1501996139; LGSID=20170806130826-47625542-7a65-11e7-bac3-525400f775ce; LGRID=20170806130858-5a80ff93-7a65-11e7-82f6-5254005c3644',
    //     'Host':'www.lagou.com',
    //     'Upgrade-Insecure-Requests':'1',
    //     'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36'
    // }, function (result) {
    //
    //     logger.access.info(result);
    // })

    // http.httpGetByURL('http://www.66ip.cn/areaindex_1/1.html', function (result) {
    //     var $ = cheerio.load(result.body);
    //     $('#main tr').each(function () {
    //         var ipList = $(this).children();
    //         if ($(ipList[0]).text() !== 'ip' && $(ipList[0]).text() !== ' ') {
    //             console.log($(ipList[0]).text() + ':' + $(ipList[1]).text());
    //         }
    //     });
    //
    // })



    var proxyList = [];

    var list = await ipListService.queryIPBySize(10000, function (result) {});

    list.forEach(function (ip) {
        proxyList.push(`${ip.ip}:${ip.port}`);
    });

    logger.access.info('通过代理来给hvaexlove.com刷流量定时任务: ' + new Date() + ";proxy.length" + proxyList.length);

    var targetOptions = {
        method: 'GET',
        url: 'http://www.hvaexlove.com/',
        timeout: 8000,
        encoding: null,
    };

    //这里修改一下，变成你要访问的目标网站
    proxyList.forEach(function (proxyurl) {

        console.log(`testing ${proxyurl}`);

        targetOptions.proxy = 'http://' + proxyurl;
        request(targetOptions, function (error, response, body) {
            try {
                if (error) throw error;


                body = body.toString();

                console.log(body);

                eval(`var ret = ${body}`);


                if (ret) {
                    console.log(`验证成功==>> ${ret.address}`);
                }
            } catch (e) {
                // console.error(e);
            }


        });

    });

})();

// function httpGet() {
//     http.httpGet("http://www.xicidaili.com/nn/", function (result) {
//         // console.info(result);
//         if (result.statusCode == 200) {
//             var $ = cheerio.load(result.body);
//             //logger.access.info(result.body);
//             $('#ip_list').find('tr').each(function () {
//                 var ipList = $(this).children();
//                 if ($(ipList[1]).text() !== 'IP地址') {
//                     logger.access.info($(ipList[1]).text() + ":" + $(ipList[2]).text() + " " + $(ipList[5]).text());
//                 }
//             });
//         } else {
//             logger.error.info(result);
//         }
//     });
// }
//
// async function addIP() {
//     ipListService.addIP({ip : '127.0.0.1', port : '80', type : 0}, function (result) {
//         logger.access.info(result);
//     })
// }

// httpGet();

// (function () {
//     console.log('代理请求');
//     http.verifyHttp('117.91.138.177', '808', function (result) {
//         console.log(result);
//     });
// })();


// (function () {
//     ipListService.getIPByStatus(0, function (ipListResult) {
//         var ipList = ipListResult;
//         ipList.forEach((ip) => {
//             http.verifyHttp(ip.ip, ip.port, function (httpResult) {
//                 if (httpResult.code === 200) {
//                     ipListService.updateByIPAndPort({ip : ip.ip, port : ip.port, status : 1, validateCount : 0}, function () {});
//                 } else {
//                     ipListService.updateByIPAndPort({ip : ip.ip, port : ip.port, status : 0, validateCount : (ip.validate_count + 1)}, function () {});
//                 }
//             });
//         });
//     });
// })();




// addIP();
//
// async function sleep(timeout) {
//     return new Promise((resolve, reject) => {
//         setTimeout(function () {
//             ipListService.addIP({ip : '127.0.0.1', port : '80'}, function (result) {
//                 resolve();
//             })
//         }, timeout);
//     });
// }
//
// (async function () {
//     console.log('Do some thing, ' + new Date());
//     await sleep(3000);
//     console.log('Do other things, ' + new Date());
// })();